package cate.game.common.scanner;

import easy.java.runtime.ClassScanner;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class GameScanner {
    private static final Map<String, IScanner> SCANNERS = new HashMap<>();
    public static final String MSG_PACKAGE = "cate.game";
    private GameScanner() {

    }

    public static void registScanner(IScanner scanner) {
        if (SCANNERS.containsKey(scanner.getClass().getName())) {
            return;
        }
        SCANNERS.put(scanner.getClass().getName(), scanner);
    }

    public static void scan() {
        long startTime = System.currentTimeMillis();
        Set<Class<?>> classes = new ClassScanner().getClasses(MSG_PACKAGE, true);
        log.info("扫描出{}个类", classes.size());
        for (Class<?> clz : classes) {
            for (IScanner scanner : SCANNERS.values()) {
                if (scanner.match(clz)) {
                    scanner.handle(clz);
                }
            }
        }
        SCANNERS.values().forEach(IScanner::afterScan);
        log.info("scan完成, 耗时：{}ms", System.currentTimeMillis() - startTime);
    }
}
